package com.qbw.app.module.home.holder;

import android.content.Context;
import android.view.ViewGroup;

import com.qbw.app.R;
import com.qbw.app.databinding.HomeHolderStatusItemBinding;
import com.qbw.app.model.Coolant;
import com.qbw.app.model.HomeStatus;
import com.qbw.app.model.ProblemStatus;
import com.qbw.app.model.TireData;
import com.qbw.app.model.Voltage;
import com.qbw.app.module.common.holder.CommonHolder;
import com.qbw.app.module.vehicle.VehicleDataBindingAdapter;
import com.qbw.app.module.vehicle.activity.VehicleCoolantActivity;
import com.qbw.app.module.vehicle.activity.VehicleProblemActivity;
import com.qbw.app.module.vehicle.activity.VehicleTireActivity;
import com.qbw.app.module.vehicle.activity.VehicleVoltageActivity;
import com.qbw.core.model.ViewType;

/**
 * @author xiansx
 * @date 2020/10/12
 */
public class HomeStatusItemHolder extends CommonHolder<ViewType, HomeHolderStatusItemBinding> {

    public HomeStatusItemHolder(Context context, ViewGroup viewParent) {
        super(context, R.layout.home_holder_status_item, viewParent);
    }

    @Override
    public void bindData(int position, ViewType data) {
        super.bindData(position, data);
        Object d = mData.getO();
        if (d instanceof Voltage) {
            Voltage voltage = (Voltage) d;
            VehicleDataBindingAdapter.setVoltageStatusText(mBinding.tvStatus, voltage.getState());
            VehicleDataBindingAdapter.setVoltageStatusDrawable(mBinding.ivType, voltage.getState());

            mBinding.tvType.setText(R.string.voltage);
        } else if (d instanceof Coolant) {
            Coolant coolant = (Coolant) d;
            VehicleDataBindingAdapter.setCoolantStatusText(mBinding.tvStatus, coolant.getState());
            VehicleDataBindingAdapter.setCoolantStatusDrawable(mBinding.ivType, coolant.getState());
            mBinding.tvType.setText(R.string.engine);
        } else if (d instanceof ProblemStatus) {
            ProblemStatus status = (ProblemStatus) d;
            mBinding.tvStatus.setText(String.valueOf(status.getValue()));
            if(status.getValue() > 0) {
                mBinding.tvStatus.setTextColor(mContext.getResources().getColor(R.color.nm_exception));
            } else {
                mBinding.tvStatus.setTextColor(mContext.getResources().getColor(R.color.nm_normal));
            }
            VehicleDataBindingAdapter.setFaultStatusDrawable(mBinding.ivType,
                                                             status.getState(),
                                                             status.getValue());
            mBinding.tvType.setText(R.string.vehicle_problem);
        } else if (d instanceof TireData) {
            TireData tireData = (TireData) d;
            int frontWheelState = tireData.getFrontWheelState(); // 前轮状态，1：正常，2：异常
            int backWheelState = tireData.getBackWheelState(); // 后轮状态，1：正常，2：异常
            if(frontWheelState == TireData.TIRE_STATE_ERROR || backWheelState == TireData.TIRE_STATE_ERROR) {
                mBinding.tvStatus.setText(R.string.exception);
                mBinding.tvStatus.setTextColor(mContext.getResources().getColor(R.color.nm_exception));
                mBinding.ivType.setImageResource(R.drawable.nm_icon_tire_red);
            } else {
                mBinding.tvStatus.setText(R.string.voltage_normal);
                mBinding.tvStatus.setTextColor(mContext.getResources().getColor(R.color.nm_normal));
                mBinding.ivType.setImageResource(R.drawable.nm_icon_tire_normal);
            }
            mBinding.tvType.setText(R.string.vehicle_tire);
        }
        itemView.setOnClickListener(mClickListenerItem);
    }

    @Override
    protected void onClickItem() {
        super.onClickItem();
        Object d = mData.getO();
        if (d instanceof Voltage) {
            Voltage voltage = (Voltage) d;
            if (voltage.getVehicle() != null) {
                VehicleVoltageActivity.start(mContext, voltage);
            }
        } else if (d instanceof Coolant) {
            Coolant coolant = (Coolant) d;
            if (coolant.getVehicle() != null) {
                VehicleCoolantActivity.start(mContext, coolant);
            }
        } else if (d instanceof ProblemStatus) {
            ProblemStatus status = (ProblemStatus) d;
            if (status.getVehicle() != null) {
                VehicleProblemActivity.startActivity(mContext, status);
            }
        } else if (d instanceof TireData) {
            TireData tireData = (TireData) d;
            if (tireData.getVehicle() != null) {
                VehicleTireActivity.startActivity(mContext, tireData.getVehicle());
            }
        }
    }

}
